using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using Microsoft.VisualStudio.DebuggerVisualizers;
using Microsoft.SharePoint;
using System.Data;

namespace SPDV
{
    public class SPListItemObjectProvider : VisualizerObjectSource
    {
        public override void GetData(object target, System.IO.Stream outgoingData)
        {
            BinaryFormatter formatter = new BinaryFormatter();
            formatter.Serialize(outgoingData, GetSerializableObject((SPListItem)target));
        }
        private SPListItemData GetSerializableObject(SPListItem listItem)
        {
            if (null == listItem) return null;
            SPListItemData splid = new SPListItemData();
            splid.Title = listItem.ParentList.Title;
            splid.ID = listItem.ParentList.ID;
            //Whatch out when using SPField.Title, duplicates might appear
            foreach (SPField f in listItem.Fields)
                splid.content.Columns.Add(f.InternalName);
            DataRow dr = splid.content.NewRow();
            foreach (DataColumn dc in splid.content.Columns)
                dr[dc.ColumnName] = listItem[dc.ColumnName];
            splid.content.Rows.Add(dr);
            return splid;
        }
    }
}
